import 'package:flutter_frame/common/const.dart';
import 'package:flutter_frame/page/login/bean/user_model.dart';
import 'package:flutter_frame/utils/util_export.dart';

class UserManager {
  static final UserManager _instance = UserManager._internal();

  factory UserManager() => _instance;

  // 私有的命名构造函数
  UserManager._internal();

  UserModel _userModel = UserModel();

  saveData(UserModel userModel) async {
    await SPreUtils.setString(userIdKey, userModel.userId);
    await SPreUtils.setString(userMobileKey, userModel.mobile);
    await SPreUtils.setString(userTokenKey, userModel.token);
  }

  initData() async {
    var userId = await SPreUtils.getString(userIdKey);
    var token = await SPreUtils.getString(userTokenKey);
    var mobile = await SPreUtils.getString(userMobileKey);
    _userModel = UserModel(userId: userId, token: token, mobile: mobile);
  }

  isLogin() {
    return _userModel.token.isNotEmpty;
  }

  getToken() {
    return _userModel.token;
  }

  logout() {
    saveData(UserModel(userId: "", token: "", mobile: ""));
  }
}
